package ch.sv7.tool.dbmanager.db.ant;

import org.apache.tools.ant.BuildException;

import ch.sv7.tool.dbmanager.DbManagerException;
import ch.sv7.tool.dbmanager.db.DatabaseSchema;
import ch.sv7.tool.dbmanager.db.xml.XmlDatabaseSchemaExport;

public class Schema2XmlTask extends TablesSqlTask {

	private String filename;

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void execute() throws BuildException {
		try {
			java.sql.Connection conn = getConnection();
			try {
				log("Extracting Structure informations");
				DatabaseSchema dbSchema = getDatabaseSchema(conn, getTables());
				XmlDatabaseSchemaExport exporter = new XmlDatabaseSchemaExport();
				exporter.exportToFile(filename, dbSchema);
				log("Structure informations saved in : " + filename);
			} catch (Exception e) {
				e.printStackTrace();
				throw new BuildException(e, getLocation());
			} finally {
				closeConnection(conn);
			}
		} catch (DbManagerException e) {
			throw new BuildException(e, getLocation());
		}
	}
}
